package com.hsurosy.domain.strategy.service.rule.tree.factory;

import com.hsurosy.domain.strategy.model.valobj.RuleLogicCheckTypeVO;
import com.hsurosy.domain.strategy.model.valobj.RuleTreeVO;
import com.hsurosy.domain.strategy.service.rule.tree.ILogicTreeNode;
import com.hsurosy.domain.strategy.service.rule.tree.factory.engine.IDecisionTreeEngine;
import com.hsurosy.domain.strategy.service.rule.tree.factory.engine.impl.DecisionTreeEngine;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Author Hsu琛君珩
 * @Date 2024-10-19 15:29
 * @Description 规则树工厂
 * @Version: v1.0.0
 */
@Service
public class DefaultTreeFactory {

    // 逻辑树节点的集合，key为节点的标识符，value为具体的节点逻辑实现类
    private final Map<String, ILogicTreeNode> logicTreeNodeGroup;

    // 构造函数，注入逻辑树节点集合
    public DefaultTreeFactory(Map<String, ILogicTreeNode> logicTreeNodeGroup) {
        this.logicTreeNodeGroup = logicTreeNodeGroup;
    }

    /**
     * 打开一个规则树引擎，用于执行规则树的逻辑
     *
     * @param ruleTreeVO 规则树对象，包含规则树的结构和节点信息
     * @return 返回决策树引擎接口，供外部调用规则树逻辑
     */
    public IDecisionTreeEngine openLogicTree(RuleTreeVO ruleTreeVO) {
        // 通过规则树对象和节点集合创建并返回决策树引擎
        return new DecisionTreeEngine(logicTreeNodeGroup, ruleTreeVO);
    }

    /**
     * 决策树动作的结果实体类，表示规则树节点执行后的动作结果
     */
    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class TreeActionEntity {
        // 规则逻辑的检查类型，表示是否通过规则逻辑或需要接管
        private RuleLogicCheckTypeVO ruleLogicCheckType;
        // 奖品数据，包含奖品ID和规则信息
        private StrategyAwardVO strategyAwardVO;
    }

    /**
     * 奖品数据实体类，包含抽奖相关的奖品信息
     */
    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class StrategyAwardVO {
        /** 抽奖奖品ID - 用于内部流转的奖品ID */
        private Integer awardId;
        /** 抽奖奖品规则 - 该奖品的规则值 */
        private String awardRuleValue;
    }

}
